package com.zero.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zero.pojo.Users;
import com.zero.pojo.vo.UsersVO;
import com.zero.service.UserService;
import com.zero.utils.MD5Utils;
import com.zero.utils.ZeroJSONResult;

@RestController
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;

	@PostMapping("registOrLogin")
	public ZeroJSONResult registOrLogin(@RequestBody Users user) throws Exception {

		if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
			return ZeroJSONResult.errorMsg("账号或密码不能为空");
		}
		Users loginUser = null;
		boolean userIsExist = userService.queryUserName(user.getUsername());
		if (userIsExist) {
			loginUser = userService.queryLoginUser(user);
			if (loginUser == null) {
				return ZeroJSONResult.errorMsg("账号或密码错误");
			}
		} else {
		
			loginUser=userService.savaUser(user);
		}
		UsersVO uservo = new UsersVO();
		BeanUtils.copyProperties(loginUser, uservo);
		return ZeroJSONResult.ok(loginUser);
	}

}
